<template>
  <div class='container'>
    <el-card>
      <div class="page-top">
        <el-form :inline="true">
          <el-form-item label="关键字">
            <el-input v-model="keyword" placeholder="关键字" size="small"></el-input>
          </el-form-item>
        </el-form>
        <div style="float: right">
          <el-button size="small" @click="clear()">清除</el-button>
          <el-button size="small" type="primary" @click="filter()">搜索</el-button>
        </div>
      </div>
      <!-- 这个是设置空值 close-text=" " 必须写文本,但题目不需要文本，就设置成空值了。和 :closable="false"效果一样-->
      <div class="tips">
          <el-alert
          :title="`数据一共${total}条`"
          type="info"
          :closable="false"
          show-icon>
          </el-alert>
          </div>

          <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            prop="id"
            label="编号"
            width="200">
          </el-table-column>
          <el-table-column
            prop="questionType"
            label="题型"
            width="100"
            :formatter="formatType"
            >
          </el-table-column>
          <el-table-column
            label="题目编号"
            width="200">
            <template slot-scope="scope">
              <el-button style="margin: 0;" type="text" v-for="item in scope.row.questionIDs" :key="item.number"  @click="viewDetail(item)">{{item.number}}</el-button>
            </template>
          </el-table-column>
          <el-table-column
            prop="addTime"
            label="录入时间"
            width="200"
            >
          </el-table-column>
          <el-table-column
            prop="totalSeconds"
            label="答题时间(s)">
          </el-table-column>
          <el-table-column
            prop="accuracyRate"
            label="正确率(%)">
          </el-table-column>
          <el-table-column
            prop="userName"
            label="录入人">
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button type="danger" plain icon="el-icon-delete"  size="small" circle @click="delQuestion(scope.row)" title="删除"></el-button>
            </template>
          </el-table-column>
        </el-table>
          <!-- 分页功能-->
           <el-pagination
            background
            style="text-align:right;margin-top:20px"
            :page-sizes="[20, 30, 50, 100]"
            layout="prev, pager, next, sizes, jumper"
            :page-size="pagesize"
            :total="total"
            :current-page="currentpage"
            @current-change = "handleCurrentChange"
            @size-change="handleSizeChange">
            <!-- current-change  current-page改变时会触发 -->
          </el-pagination>
        <QuestionsPreview :dialogVisible.sync="dialogVisible" :titleId="titleId" :Titledetil="Titledetil" :options="options"></QuestionsPreview>
    </el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions.js'
import QuestionsPreview from '@/module-hmmm/components/questions-preview'
// import { request } from 'screenfull'
export default {
  data () {
    return {
      keyword: null,
      dialogVisible: false,
      titleId: 0,
      Titledetil: {},
      options: [],
      tableData: [],
      total: 0,
      pagesize: 20,
      currentpage: 1
    }
  },
  components: {
    QuestionsPreview
  },
  // created通常放在data下面
  created () {
    this.getRandoms()
  },
  methods: {
    clear () {
      this.keyword = null
    },
    filter () {
      this.getRandoms()
    },
    formatType (row) {
      if (+row.questionType === 1) {
        return '单选'
      } else if (+row.questionType === 2) {
        return '多选'
      } else {
        return '简答'
      }
    },
    // 获取组题的列表
    async getRandoms () {
      const res = await randoms({ page: this.currentpage, pagesize: this.pagesize, keyword: this.keyword })
      // console.log(res)
      this.tableData = res.data.items
      // console.log(this.tableData)
      this.total = res.data.counts
      // console.log(this.total)
    },
    // 分页监听方法 放此方法执行时重新获取列表
    handleCurrentChange (val) {
      // console.log(`当前页：${val}`)
      this.currentpage = val // val参数为分到第几页
      this.getRandoms() // 获取组题的列赋值给data列表
    },
    handleSizeChange (val) {
      // console.log(`每页${val}条`)
      this.pagesize = val
      this.getRandoms()
    },
    async delQuestion (question) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await removeRandoms({ id: question.id })
      this.$message.success('删除成功')
      this.getRandoms()
    },
    viewDetail (item) {
      this.dialogVisible = true
      detail({ id: item.id }).then(res => {
        // console.log(res.data)
        this.options = res.data.options
        // this.Titledetil.videoURL = res.data.videoURL
        // this.Titledetil.answer = res.data.answer
        // this.Titledetil.remarks = res.data.remarks
        this.Titledetil = res.data
      })
    }
  }

}
</script>
<style scoped lang='scss'>
  .page-top{
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .tips{
    margin-bottom: 10px;
  }
</style>
